package ce.com.newbluedemo.my;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;

import java.util.ArrayList;

import ce.com.cenewbluesdk.CEBC;
import ce.com.cenewbluesdk.entity.CEDevData;
import ce.com.cenewbluesdk.entity.k2.K2_MixInfoStruct;
import ce.com.cenewbluesdk.entity.k2.K2_SendAlarmInfoStruct;
import ce.com.cenewbluesdk.entity.k2.K2_SendContacts;
import ce.com.cenewbluesdk.entity.k2.K2_SendDevSettingStruct;
import ce.com.cenewbluesdk.entity.k2.K2_SendGoal;
import ce.com.cenewbluesdk.entity.k2.K2_SendNote;
import ce.com.cenewbluesdk.entity.k2.K2_SendUserInfo;
import ce.com.cenewbluesdk.entity.k2.K2_SendWeatherStruct;
import ce.com.cenewbluesdk.entity.k2.K2_Send_Watch_Face_And_Notification_Set;
import ce.com.cenewbluesdk.entity.k2.K2_UserCard;
import ce.com.cenewbluesdk.entity.k3.Send_Alarm_Info_Struct_K3;
import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.cenewbluesdk.uitl.Lg;
import ce.com.cenewbluesdk.uitl.TimeUtil;
import ce.com.newbluedemo.R;
import ce.com.newbluedemo.k2.K2BaseActivity;

/**
 * Created by Administrator on 2016/11/15 0015.
 */
public class TestK2BlueActivity_bast extends MyActivity implements View.OnClickListener {
    private TextView blue_state;

    private CEDevK2Proxy k2Proxy;
    private K2_UserCard userCard;

    ArrayList<K2_SendContacts> contactses = new ArrayList<>();
    int sendIndex;


    private boolean gskaiguang = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivty_test);
        k2Proxy = BleFactory.getInstance().getK2Proxy();
        blue_state = (TextView) findViewById(R.id.blue_state);
        if (k2Proxy.isConnectOK()) {
            blue_state.setText("蓝牙连接成功");
        } else {
            blue_state.setText("蓝牙连接失败");
        }


        byte b[] = new byte[2048];
        for (int i = 0; i < 2048; i++) {
            if (i < 1024) {
                b[i] = 1;
            } else {
                b[i] = 0;
            }
        }
        userCard = new K2_UserCard(1, b);


        findViewById(R.id.send_time).setOnClickListener(this);
        findViewById(R.id.send_alarm).setOnClickListener(this);
        findViewById(R.id.get_devinfo).setOnClickListener(this);
        findViewById(R.id.send_userinfo).setOnClickListener(this);
        findViewById(R.id.get_max).setOnClickListener(this);
        findViewById(R.id.send_mix).setOnClickListener(this);
        findViewById(R.id.send_setting).setOnClickListener(this);
        findViewById(R.id.send_w).setOnClickListener(this);
        findViewById(R.id.send_m).setOnClickListener(this);
        findViewById(R.id.send_gs).setOnClickListener(this);
        findViewById(R.id.send_pd).setOnClickListener(this);
        findViewById(R.id.send_goal).setOnClickListener(this);
        findViewById(R.id.send_face).setOnClickListener(this);
        findViewById(R.id.send_note).setOnClickListener(this);
        findViewById(R.id.send_card).setOnClickListener(this);
        findViewById(R.id.send_contant).setOnClickListener(this);


        for (int j = 0; j < 200; j++) {
            K2_SendContacts send_contacts=new K2_SendContacts();
            send_contacts.setName("冯胖子"+j);
            send_contacts.setPhoneNember("12345678"+j);

            contactses.add(send_contacts);
        }


    }

    @Override
    public void receiveData(Message message) {
        if (message.what == CEBC.BLUE_CONNECT_STATE_CHANGE) {
            String s = "";
            if (message.arg1 == CEBC.BLUE_CONNECTED) {
                s = "蓝牙连接成功";
            } else if (message.arg1 == CEBC.BLUE_CONNECTING) {
                s = "蓝牙正在连接";
            } else if (message.arg1 == CEBC.BLUE_DISCONNECT) {
                s = "蓝牙连接失败";
            }
            blue_state.setText(s);
        }
    }

    @Override
    public void onClick(View v) {
        if (!k2Proxy.isConnectOK()) {
            Toast.makeText(this, "蓝牙已经断开", Toast.LENGTH_SHORT);
            return;
        }
        switch (v.getId()) {
            case R.id.send_time:

                k2Proxy.getSendHelper().sendTime();
                break;
            case R.id.get_max:

                k2Proxy.getSendHelper().getMixInfo();
                break;
            case R.id.get_devinfo:

                k2Proxy.getSendHelper().getDevInfo();
                break;
            case R.id.send_w:

                k2Proxy.getSendHelper().sendWeatherInfo(new K2_SendWeatherStruct(
                        (byte) 2, (byte) 1, (byte) 5, (byte) 0, (byte) 6, (byte) 25));
                break;
            case R.id.send_setting:
                k2Proxy.getSendHelper().sendSetting(new K2_SendDevSettingStruct((byte) 0,
                        (byte) 1, (byte) 0, (byte) 1, (byte) 1, (byte) 1));
                break;
            case R.id.send_userinfo:
                k2Proxy.getSendHelper().sendUserInfo(new K2_SendUserInfo(1000, 1, 20, 170, 50, 1));
                break;
            case R.id.send_alarm:
                K2_SendAlarmInfoStruct alarm = new K2_SendAlarmInfoStruct((byte) 1, (byte) 11, (byte) 12, (byte) 1);
                K2_SendAlarmInfoStruct alarm1 = new K2_SendAlarmInfoStruct((byte) 2, (byte) 12, (byte) 13, (byte) 0);
                K2_SendAlarmInfoStruct alarm2 = new K2_SendAlarmInfoStruct((byte) 3, (byte) 13, (byte) 14, (byte) 1);
//                Send_Alarm_Info_Struct_K3  alarm3=new Send_Alarm_Info_Struct_K3((byte)4,(byte)14,(byte)16,(byte)0);
//                Send_Alarm_Info_Struct_K3  alarm5=new Send_Alarm_Info_Struct_K3((byte)5,(byte)16,(byte)17,(byte)1);
                ArrayList<K2_SendAlarmInfoStruct> k3s = new ArrayList<>();
                k3s.add(alarm);
                k3s.add(alarm1);
                k3s.add(alarm2);
//                k3s.add(alarm3);
//                k3s.add(alarm5);

                k2Proxy.getSendHelper().sendAlarmInfo(k3s);
                break;
            case R.id.send_mix:
                ArrayList<K2_MixInfoStruct.Property> properties = new ArrayList<>();
                K2_SendAlarmInfoStruct xxx = new K2_SendAlarmInfoStruct((byte) 1, (byte) 11, (byte) 12, (byte) 1);
                K2_SendAlarmInfoStruct xxx1 = new K2_SendAlarmInfoStruct((byte) 2, (byte) 12, (byte) 13, (byte) 0);
                K2_SendAlarmInfoStruct xxx2 = new K2_SendAlarmInfoStruct((byte) 3, (byte) 13, (byte) 14, (byte) 1);
//                Send_Alarm_Info_Struct_K3  alarm3=new Send_Alarm_Info_Struct_K3((byte)4,(byte)14,(byte)16,(byte)0);
//                Send_Alarm_Info_Struct_K3  alarm5=new Send_Alarm_Info_Struct_K3((byte)5,(byte)16,(byte)17,(byte)1);
                ArrayList<K2_SendAlarmInfoStruct> xx2 = new ArrayList<>();
                xx2.add(xxx1);
                xx2.add(xxx2);
                xx2.add(xxx);
//                k3s.add(alarm3);
//                k3s.add(alarm5);
                byte[] payload = new byte[Send_Alarm_Info_Struct_K3.getItemSize() * xx2.size() + 1];
                payload[0] = (byte) xx2.size();
                for (int i = 0; i < xx2.size(); ++i) {
                    K2_SendAlarmInfoStruct this_alrm = xx2.get(i);
                    byte[] this_alarm_payload = this_alrm.getBytes();
                    System.arraycopy(this_alarm_payload, 0, payload, i * Send_Alarm_Info_Struct_K3.getItemSize() + 1, Send_Alarm_Info_Struct_K3.getItemSize());
                }
                K2_MixInfoStruct.Property ar = new K2_MixInfoStruct.Property((byte) CEBC.K2.DATA_TYPE_ALARM, payload);
                properties.add(ar);
                properties.add(new K2_MixInfoStruct.Property((byte) CEBC.K2.DATA_TYPE_USERINFO, (new K2_SendUserInfo(1000, 1, 20, 170, 50, 1)).getBytes()));

                K2_MixInfoStruct mixInfoStruct = new K2_MixInfoStruct(properties);
                k2Proxy.getSendHelper().sendMixInfo(mixInfoStruct);

                break;
            case R.id.send_m:
                k2Proxy.getSendHelper().
                        sendMessage_notice(TimeUtil.now(),
                                "冯胖子", "就是一个大忽悠，就是一个大忽悠，就是一个大忽悠就是一个大忽悠，就是一个大忽悠，就是一个大忽悠，就是一个大忽悠，就是一个大忽悠", (byte) 2);
                break;
            case R.id.send_gs:
                k2Proxy.getSendHelper().setEnableGsDataTrans(gskaiguang);
                gskaiguang = !gskaiguang;

                break;
            case R.id.send_goal:
                k2Proxy.getSendHelper().sendGoal(new K2_SendGoal(108, 208, 888888888, 888));
                gskaiguang = !gskaiguang;

                break;
            case R.id.send_pd:
                k2Proxy.getSendHelper().sendPairFinish(gskaiguang);
                gskaiguang = !gskaiguang;
                break;
            case R.id.send_face:
                JSONArray jsonArray = new JSONArray();
                for (int i = 0; i < 32; i++) {
                    if (gskaiguang) {
                        jsonArray.put(i % 2);
                    } else {
                        jsonArray.put((i + 1) % 2);
                    }
                }

                Lg.e(jsonArray.toString());
                gskaiguang = !gskaiguang;
                k2Proxy.getSendHelper().sendWatchFace(
                        K2_Send_Watch_Face_And_Notification_Set.createWatchFace(jsonArray));
                break;
            case R.id.send_note:
                K2_SendNote k2_sendNote = new K2_SendNote(100, 1, 0, (int) ((TimeUtil.now() / 1000) + 300), Integer.MAX_VALUE
                        , new int[]{1, 1, 1, 1, 1, 1, 1, 1}, 1, new int[]{1, 1, 1, 1, 1, 1, 1, 1}, "冯胖子就是个大忽悠");
                k2_sendNote.setNoteType(1);
                k2Proxy.getSendHelper().send_Note(k2_sendNote);


                break;
            case R.id.send_card:


                CEDevData d = userCard.getCeData();
                if (d != null) {
                    k2Proxy.getSendHelper().sendUserCard(d);
                } else {
                    Lg.e("数据发送完成");
                }


                break;
            case R.id.send_contant:

                if(sendIndex<contactses.size()){
                    k2Proxy.getSendHelper().sendPhoneBook(contactses,sendIndex);
                    sendIndex=sendIndex+6;
                    Log.e("liu","sendIndex="+sendIndex);
                }else{
                    Lg.e("数据发送完成");
                }


                break;
        }
    }
}
